# text = '历史名人 大苏打实打实大苏打'
# print(text.find('历史名人'))
# print('历史名人' in text)
# text = '简体'
# print(text.find('历史名人'))
# print('历史名人' in text)
# text = 'hqz，'
# print(text.strip('，'))
# content > div.arc > li > b:nth-child(3)

# !/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import time
# from openpyxl import Workbook
import random


#
# wb = Workbook()
# ws1 = wb.active
# ws1.cell(row=1, column=1).value = '测试'
# data = []
# for index in range(50):
#     data.append(random.randint(1, 50))
#
# for i in range(len(data)):
#     ws1.cell(row=i+2,column=1).value = data[i]
#
# wb.save("test_convert_2_class.xlsx")

# allData = [{'abc': [1, 2, 3, 4]}, {'def': [5, 6, 7, 8]}]
# for i in range(len(allData)):
#     for key in allData[i].keys():
#        ws1.cell(row=1,column=i+1).value = key
#     for value in allData[i].values():
#         for j in range(len(value)):
#             ws1.cell(row=j+2,column=i+1).value = value[j]


# wb.save("test_convert_2_class.xlsx")

# def split_str(rule, str):
#     strs = re.split(rule, str)
#     new_str = list(filter(lambda x: len(x) > 2, strs))
#     return new_str


# str = '1月5日　中共湖南省委、省人民政府召开省直机关农村社会主义思想教育工作总结表彰和集训动员大会。1991年，全省又组织县以上机关干部2700多人，加上区和乡镇干部5万余人组成工作队下到1.6万多个村帮助开展农村社教。 　　1月10日　新华社消息：由长沙矿山研究院湘潭电机厂等9个单位研制的目前国内载重量最大的154吨电动自卸汽车问世。'
# new_str = '1991年，全省又组织县以上机关干部2700多人，加上区和乡镇干部5万余人组成工作队下到1.6万多个村帮助开展农村社教。'
# ruleYear = r'(\d+年)'
# strs = re.split(ruleYear, new_str)
# new_str = list(filter(lambda x: len(x) > 2, strs))
# print(new_str)
def get_ymd(str_ymd):
    pattern_ymd = re.compile(r'\d+年\d+月\d+日')  # 定义匹配模式
    list = re.findall(pattern_ymd, str_ymd)
    if list.__len__() > 0:
        return list[0]
    else:
        pattern_ym = re.compile(r'\d+年\d+月')
        list = re.findall(pattern_ym, str_ymd)
        if list.__len__() > 0:
            return list[0]
        else:
            pattern_y = re.compile(r'\d+年')
            list = re.findall(pattern_y, str_ymd)
            return list[0]


print(get_ymd("1999年"))


# 时间字符串获取年月日
# str = time.strptime('1999-9-1', '%Y-%m-%d')
# strs = time.strptime('1998-9-1', '%Y-%m-%d')
# print(str.tm_year - strs.tm_year)


def format_time(str_time):
    try:
        array = time.strptime(str_time, u"%Y年%m月%d日")
        new_time = time.strftime("%Y-%m-%d", array)
        return new_time
    except Exception as e:
        try:
            array = time.strptime(str_time, u"%Y年%m月")
            new_time = time.strftime("%Y-%m", array)
            return new_time
        except Exception as ex:
            print(ex)
            try:
                array = time.strptime(str_time, u"%Y年")
                new_time = time.strftime("%Y", array)
                return new_time
            except Exception as exe:
                print(exe)
        print(e)


def format_time2(str_time):
    my_time = ''
    if str_time.find('（'):
        my_time = str_time.split('（')[0]
    else:
        my_time = str_time
    try:
        if my_time.find('日') != -1:
            array = time.strptime(my_time, u"%Y年%m月%d日")
            new_time = time.strftime("%Y-%m-%d", array)
            return new_time
        if my_time.find('月') != -1:
            array = time.strptime(my_time, u"%Y年%m月")
            new_time = time.strftime("%Y-%m", array)
            return new_time
        if my_time.find('年') != -1:
            array = time.strptime(my_time, u"%Y年")
            new_time = time.strftime("%Y", array)
            return new_time
    except Exception as e:
        return ''


def strptime(t_str):
    try:
        return time.strptime(t_str, '%Y').tm_year
    except Exception as e1:
        try:
            return time.strptime(t_str, '%Y-%m').tm_year
        except Exception as e2:
            try:
                return time.strptime(t_str, '%Y-%m-%d').tm_year
            except Exception as e3:
                return -1

# print(format_time2('1999年8月（顺治）'))
# print(strptime('1999-01-02'))
